#ifndef __LOG_SYSTEM_H__
#define __LOG_SYSTEM_H__

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "common.h"
#include <fcntl.h>
#include <unistd.h>
#define LOG_FILE "log.txt"

#define LOG_LOGIN(username) do{ \
    FILE *openfd = fopen(LOG_FILE,"a");  \
    char log_str[1024]; \
    snprintf(log_str,sizeof(log_str),"LOGIN:%s",username); \
    time_t now = time(NULL); \
    struct tm *tm_now = localtime(&now); \
    char time_str[20]; \
    strftime(time_str,sizeof(time_str),"%Y-%m-%d %H:%M:%S",tm_now); \
    fprintf(openfd,"[%s] %s\n",time_str,log_str); \
    fclose(openfd); \
}while(0)
#define LOG_COMMAND(type) do{ \
    FILE *openfd = fopen(LOG_FILE,"a");  \
    char log_str[1024]; \
    snprintf(log_str,sizeof(log_str),"%s",type); \
    time_t now = time(NULL); \
    struct tm *tm_now = localtime(&now); \
    char time_str[20]; \
    strftime(time_str,sizeof(time_str),"%Y-%m-%d %H:%M:%S",tm_now); \
    fprintf(openfd,"[%s] %s\n",time_str,log_str); \
    fclose(openfd); \
}while(0)

#define LOG_COMMAND1(type,arg1) do{ \
    FILE *openfd = fopen(LOG_FILE,"a");  \
    char log_str[1024]; \
    snprintf(log_str,sizeof(log_str),"%s %s",type,arg1); \
    time_t now = time(NULL); \
    struct tm *tm_now = localtime(&now); \
    char time_str[20]; \
    strftime(time_str,sizeof(time_str),"%Y-%m-%d %H:%M:%S",tm_now); \
    fprintf(openfd,"[%s] %s\n",time_str,log_str); \
    fclose(openfd); \
}while(0)
#define LOG_ERROR(error) do{ \
    FILE *openfd = fopen(LOG_FILE,"a");  \
    char log_str[1024]; \
    snprintf(log_str,sizeof(log_str),"ERROR: %s",error); \
    time_t now = time(NULL); \
    struct tm *tm_now = localtime(&now); \
    char time_str[20]; \
    strftime(time_str,sizeof(time_str),"%Y-%m-%d %H:%M:%S",tm_now); \
    fprintf(openfd,"[%s] %s\n",time_str,log_str); \
    fclose(openfd); \
}while(0)

#endif